package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;


@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 参数：
     * 1. Message 原生消息详细信息
     * 2. T<发送的消息的类型>
     * 3. Channel channel 当前传输数据的通道
     *
     * @param message
     * @param content
     * @param channel
     */
//    @RabbitListener(queues = "hello-java-queue")
    public void recieveMessage(Message message,
                               OrderReturnReasonEntity content,
                               Channel channel) {
        byte[] body = message.getBody();
        System.out.println("接收到消息：" + message + "===>内容：" + content);
    }

    @RabbitHandler
    public void reciveMessage2(Message message, OrderReturnReasonEntity content, Channel channel) throws IOException {
        System.out.println(content);
        // 自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        if (deliveryTag % 2 == 0) {
            channel.basicAck(deliveryTag, false);
        }else{
            System.out.println("未手动确认");
            /**
             * 参数1： 标记
             * 参数2： 是否批量拒绝
             * 参数3： 是否重新入队
             */
            channel.basicNack(deliveryTag,false,true);

//            参数1：标记    参数2：是否重新入队
//            channel.basicReject(deliveryTag,false);
        }
    }

    @RabbitHandler
    public void reciveNessage3(Message message, OrderEntity entity, Channel channel) throws IOException {
        System.out.println(entity);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}